package aitsm;

import java.awt.HeadlessException;
import java.beans.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author minh
 */
public class LoginStaff {
    
    XMLConfig con = new XMLConfig();
    Connect_class cClass = new Connect_class();
    ProcessPublic pp = new ProcessPublic();
    public static boolean rule;
    public static boolean state;
    
    public void showStaffAdmin(){
        String SQL =" Select  s.staff_id as username, s.personal_id, s.staff_first_name as FirstName ,s.staff_last_name as LastName ,s.birthday,"+
                        " s.gender,s.address,s.phone_number as Phone,s.staff_state, r.password, r.rules , s.techology_id " +
                        " from staff s"+
                        " left outer join [Rule] r on s.staff_id = r.staff_id " ;
            ResultSet rs = cClass.getRecordSet(SQL);
            
            pp.JTableCheckBoxRecordSet(Main_Screen.tb_staff, rs);
//            pp.ChangeName (Main_Screen.tb_staff, 0, "User Name");
//            pp.ChangeName (Main_Screen.tb_staff, 2, "First Name");
//            pp.ChangeName (Main_Screen.tb_staff, 3, "Last Name");
//            pp.ChangeName (Main_Screen.tb_staff, 7, "Phone");
//            pp.ChangeName (Main_Screen.tb_staff, 8, "State");
//            pp.ChangeName (Main_Screen.tb_staff, 11, "Techology");
//            pp.ChangeName (Main_Screen.tb_staff, 12, "Subject");
            for ( int i = 0 ; i < Main_Screen.tb_staff.getRowCount(); i ++) {
                if (Main_Screen.tb_staff.getValueAt(i,5).equals("1") ){
                    Main_Screen.tb_staff.setValueAt("Male", i, 5);
                }else{
                    Main_Screen.tb_staff.setValueAt("Fee Male", i, 5);
                }
                if (Main_Screen.tb_staff.getValueAt(i,8).equals("0")){
                    Main_Screen.tb_staff.setValueAt("unemployed", i, 8);
                }else{
                    Main_Screen.tb_staff.setValueAt("Working", i, 8);
                }
                if (Main_Screen.tb_staff.getValueAt(i,9) == null) {
                    Main_Screen.tb_staff.setValueAt("NULL", i, 9);
                }
                if (Main_Screen.tb_staff.getValueAt(i,10) == null) {
                    Main_Screen.tb_staff.setValueAt("NULL", i, 10);
                }
                if (Main_Screen.tb_staff.getValueAt(i,10).equals("1")) {
                    Main_Screen.tb_staff.setValueAt("Staff", i, 10);
                }
                if (Main_Screen.tb_staff.getValueAt(i,10).equals("0")){
                    Main_Screen.tb_staff.setValueAt("Admin", i, 10);
                }
                if (Main_Screen.tb_staff.getValueAt(i,11) == null) {
                    Main_Screen.tb_staff.setValueAt("NULL", i, 11);
                }
            }
    }
    
    public void login (String username, String password){
        if (username.length()>0 && password.toString().length()>0) {            
            if (con.con != null) {
                try {
                    String SQL = "Select *  FROM [AITSM].[dbo].[Rule]  where  staff_id = '"+ username +"' ";    
                    ResultSet rs = cClass.getRecordSet(SQL);

                    if (rs.next()) {
                        ResultSet rsStaff = cClass.getRecordSet( "select staff_state from staff where staff_id = '"+ username +"' ");
                        boolean sStaff_State = false;
                        if(rsStaff.next()){
                            sStaff_State = rsStaff.getBoolean("staff_state");
                        }
                        rule = rs.getBoolean(4);
                        state = rs.getBoolean(3);
                        if (rs.getString("password").toString().equals(password) ) {
                            if (sStaff_State) {
                               
                                if (state) {
                                    JOptionPane.showMessageDialog(null, "the account is log in another!", "Login", JOptionPane.WHEN_FOCUSED);
                                }else if(!rule){// login admin
                                    JOptionPane.showMessageDialog(null, "Login successful!","Login", JOptionPane.WIDTH);
                                    Main_Screen.TabbedPane_main.setEnabled(true);
                                    Main_Screen.TabbedPane_main.setSelectedIndex(1);
                                    Main_Screen.TabbedPane_main.setEnabledAt(0, false);                    
                                }                                
                            }else JOptionPane.showMessageDialog(null, "UserName has removed!","Login", JOptionPane.WHEN_FOCUSED);
                            
                        }else JOptionPane.showMessageDialog(null, "Password is not correct!","Login", JOptionPane.WHEN_FOCUSED);
                    }
                    else{
                        JOptionPane.showMessageDialog(null, "UserName  is not exist!","Login", JOptionPane.WHEN_FOCUSED);
                    }
                    
                }catch (SQLException ex) {
                    ex.getMessage();
                    Logger.getLogger(LoginStaff.class.getName()).log(Level.SEVERE, null, ex);                    
                }
            }
        }else JOptionPane.showMessageDialog(null, "UserName and password cannot be blank!","Login", JOptionPane.WHEN_FOCUSED);
    }

    
}
